package com.wl.service.Impl;

import com.wl.mapper.AIChatMapper;
import com.wl.pojo.AIChat.ChatHistory;
import com.wl.service.AIChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AIChatServiceImpl implements AIChatService {

    @Autowired
    private AIChatMapper aiChatMapper;

    /**
     * 根据用户ID获取聊天记录
     * @param userId 用户id
     * @return 返回聊天记录列表
     */
    @Override
    public List<ChatHistory> getChatHistoryDatas(String userId) {
        return aiChatMapper.getChatHistoryDatas(userId);
    }

    /**
     * 修改聊天记录标题
     */
    @Override
    public void updateChatHistoryTitle(ChatHistory chatHistory) {
        aiChatMapper.updateChatHistoryTitle(chatHistory);
    }

    /**
     * 删除聊天记录
     */
    @Override
    public void deleteChatHistory(Integer chatHistoryId) {
        aiChatMapper.deleteChatHistory(chatHistoryId);
    }

    /**
     * 保存聊天记录并立即返回
     */
    @Override
    @Transactional
    public ChatHistory saveChatHistory(ChatHistory chatHistory) {
        // 获取时间
        LocalDateTime now = LocalDateTime.now();
        chatHistory.setChatDate(now);
        // 保存聊天记录
        aiChatMapper.saveChatHistory(chatHistory);
        // 返回插入的这条聊天记录
        return aiChatMapper.returnSaveChatHistory(chatHistory.getChatHistoryId());
    }

    /**
     * 更新聊天记录内容
     */
    @Override
    public void updateChatHistoryMessages(ChatHistory chatHistory) {
        aiChatMapper.updateChatHistoryMessages(chatHistory);
    }

}
